package sy.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.SupplierOffer;
import sy.service.SupplierOfferServiceI;

@Controller
@RequestMapping("/supplierOfferController")
public class SupplierOfferController {

	private SupplierOfferServiceI supplierOfferService;

	public SupplierOfferServiceI getSupplierOfferService() {
		return supplierOfferService;
	}

	@Autowired
	public void setSupplierOfferService(SupplierOfferServiceI supplierOfferService) {
		this.supplierOfferService = supplierOfferService;
	}

	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@RequestMapping("/reg")
	@ResponseBody
	public Json reg(SupplierOffer supplierOffer) {
		Json j = new Json();
		try {
			SupplierOffer offer = supplierOfferService.save(supplierOffer);
			j.setSuccess(true);
			j.setMsg("注册成功！");
			j.setObj(offer);
		} catch (Exception e) {
			j.setMsg("注册失败！用户名已存在！");
		}
		return j;
	}

	@RequestMapping("/combogrid")
	@ResponseBody
	public DataGrid combogrid(SupplierOffer supplierOffer) {
		System.out.println("测试***combogrid");
		return supplierOfferService.datagrid(supplierOffer);
	}

	@RequestMapping("/combobox")
	@ResponseBody
	public List<SupplierOffer> combobox(SupplierOffer supplierOffer) {
		return supplierOfferService.combobox(supplierOffer);
	}

	@RequestMapping("/datagrid")
	@ResponseBody
	public DataGrid datagrid(SupplierOffer supplierOffer) {
		System.out.println("测试datagrid***");
		return supplierOfferService.datagrid(supplierOffer);
	}

	@RequestMapping("/remove")
	@ResponseBody
	public Json remove(SupplierOffer supplierOffer) {
		System.out.println("/remove******delete");
		supplierOfferService.remove(supplierOffer.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		return j;
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(SupplierOffer supplierOffer) {
		Json j = new Json();
		try {
			System.out.println("/add****");
			SupplierOffer supplier = supplierOfferService.save(supplierOffer);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(supplier);
		} catch (Exception e) {
			j.setMsg("添加失败！供应商已存在！");
		}
		return j;
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(SupplierOffer supplierOffer) {
		System.out.println("edit*000*********");
		Json j = new Json();
		try {
			SupplierOffer supplier = supplierOfferService.edit(supplierOffer);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setObj(supplier);
		} catch (Exception e) {
			j.setMsg("编辑失败！用户名已存在！");
		}
		return j;
	}

/*	@RequestMapping("/modifyRole")
	@ResponseBody
	public Json modifyRole(SupplierOffer SupplierOffer) {
		Json j = new Json();
		SupplierOfferService.modifyRole(SupplierOffer);
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		return j;
	}
*/
	/*	@RequestMapping("/modifyPwd")
	@ResponseBody
	public Json modifyPwd(SupplierOffer SupplierOffer) {
		Json j = new Json();
		SupplierOfferService.modifyPwd(SupplierOffer);
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		return j;
	}*/

/*	@RequestMapping("/modifySupplierOffer")
	@ResponseBody
	public Json modifyCurrentSupplierOfferPwd(SupplierOffer SupplierOffer) {
		Json j = new Json();
		supplierOfferService.modifyPwd(supplierOffer);
		j.setSuccess(true);
		j.setMsg("修改成功！");
		return j;
	}*/
/*
	@RequestMapping("/supplierOfferOffer")
	public String supplierOfferOffer() {
		return "supplierOffer/supplierOfferOffer";
	}*/

}
